/**
 * @file:          DebugPrint.c
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.08.08
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.08.08,16:24:00
 */

/* Include Files **************************************************************/
#include "DebugPrint.h"
#include "main.h"
#include "base_math.h"
#include "string.h"

/* Global Variable Define *****************************************************/
char DebugIndex = 0;
char DebugBuff[1][UART_TX_BUFF_SIZE] = {0};
uint16_t DebugBuff_Len;
char DebugNewDataBuff[UART_TX_BUFF_SIZE] = {0};
uint16_t DebugNewDataBuff_Len;
char DebugTmpBuff[UART_TX_BUFF_SIZE] = {0};
uint16 DebugBaseIndex = 0;
char float_str_buff[FLOAT2STR_CHANNEL_SIZE][15];
uint16_t num_loop = 0;

void DBprintfDMA_Send()
{
    stepper_motor_controller.mcu->DMA_Send();
}

void DBprintfDMAAdv_Send()
{
    stepper_motor_controller.mcu->DMAAdv_Send();
}

void DBprintfDMAAdv_AddStr(char *str)
{
#ifdef DEBUG_PRINT

#ifndef HARDWARE_DISABLE
    uint16_t len = strlen(str);
    if (DebugNewDataBuff_Len + len <= UART_TX_BUFF_SIZE - 1)
    {
        memcpy(&DebugNewDataBuff[DebugNewDataBuff_Len], str, len);
        DebugNewDataBuff_Len += len;
    }
#else
    printf(str);
#endif

#endif
}

void DBprintfDMAAdv_AddStr_N(char *str, uint16_t n)
{
#ifdef DEBUG_PRINT

#ifndef HARDWARE_DISABLE
    if (DebugNewDataBuff_Len + n <= UART_TX_BUFF_SIZE - 1)
    {
        memcpy(&DebugNewDataBuff[DebugNewDataBuff_Len], str, n);
        DebugNewDataBuff_Len += n;
    }
#else
    printf(str);
#endif

#endif
}

void HexAddByte(uint8_t byte)
{
    if (DebugNewDataBuff_Len + 1 <= UART_TX_BUFF_SIZE - 1)
    {
        memcpy(&DebugNewDataBuff[DebugNewDataBuff_Len], &byte, 1);
        DebugNewDataBuff_Len += 1;
    }
}

void HexAddArray(uint8_t *data, uint8_t len)
{
    if (DebugNewDataBuff_Len + len <= UART_TX_BUFF_SIZE - 1)
    {
        memcpy(&DebugNewDataBuff[DebugNewDataBuff_Len], data, len);
        DebugNewDataBuff_Len += len;
    }
}

char *float2str(float data_float, uint16_t disp_bits)
{
    int32 f32_a;
    int32 f32_b;
    uint32_t disp_gains = 1;

    num_loop = (num_loop + 1) % FLOAT2STR_CHANNEL_SIZE;

    for (uint16_t i = 0; i < disp_bits; i++)
    {
        disp_gains *= 10;
    }

    f32_a = (int32)abs_bm(data_float);
    f32_b = (int32)((abs_bm(data_float) - f32_a) * disp_gains);

    switch (disp_bits)
    {
    case 1:
        sprintf(float_str_buff[num_loop], "-%d.%01d", f32_a, f32_b);
        break;
    case 2:
        sprintf(float_str_buff[num_loop], "-%d.%02d", f32_a, f32_b);
        break;
    case 3:
        sprintf(float_str_buff[num_loop], "-%d.%03d", f32_a, f32_b);
        break;
    case 4:
        sprintf(float_str_buff[num_loop], "-%d.%04d", f32_a, f32_b);
        break;
    case 5:
        sprintf(float_str_buff[num_loop], "-%d.%05d", f32_a, f32_b);
        break;
    case 6:
        sprintf(float_str_buff[num_loop], "-%d.%06d", f32_a, f32_b);
        break;
    default:
        break;
    }

    if (data_float >= 0)
    {
        strcpy(float_str_buff[num_loop], float_str_buff[num_loop] + 1);
    }

    return float_str_buff[num_loop];
}